#include "CreateUserWindow.h"
#include "main.h"
#include "AdminManager.h"

CreateUserWindow::CreateUserWindow()
{
	setWindowIcon(QIcon("./images/Agora.png"));
	display();
	this->show();
}

void CreateUserWindow::keyPressEvent(QKeyEvent *e)
{
	if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter)
		createUser();
	QWidget::keyPressEvent(e);
}

void		CreateUserWindow::display()
{
	this->setMinimumSize(320, 170);
	this->setMaximumSize(320, 170);
			//IP GroupBox
	QGroupBox *userInfoBox = new QGroupBox(this);
	userInfoBox->setTitle("User's Informations");
	userInfoBox->setGeometry(10, 10, 300, 120);
	QVBoxLayout	*userInfoVBox = new QVBoxLayout();

	QLabel *userNameLabel = new QLabel(tr("Name : "));
	_userName = new QLineEdit();
	QHBoxLayout *userNameHBox = new QHBoxLayout();
	userNameHBox->addWidget(userNameLabel);
	userNameHBox->addWidget(_userName);

	QLabel *userPassLabel = new QLabel(tr("Password : "));
	_userPass = new QLineEdit();
	_userPass->setEchoMode(QLineEdit::Password);
	QHBoxLayout *userPassHBox = new QHBoxLayout();
	userPassHBox->addWidget(userPassLabel);
	userPassHBox->addWidget(_userPass);
	QLabel *userConPassLabel = new QLabel(tr("Confirm Password : "));
	_userConPass = new QLineEdit();
	_userConPass->setEchoMode(QLineEdit::Password);
	QHBoxLayout *userConPassHBox = new QHBoxLayout();
	userConPassHBox->addWidget(userConPassLabel);
	userConPassHBox->addWidget(_userConPass);

	userInfoVBox->addLayout(userNameHBox);
	userInfoVBox->addLayout(userPassHBox);
	userInfoVBox->addLayout(userConPassHBox);
	userInfoBox->setLayout(userInfoVBox);

	QPushButton* buttonCreate = new QPushButton("Create", this);
	buttonCreate->setGeometry(10, 130, 300, 30);

	connect(buttonCreate, SIGNAL(clicked()), this, SLOT(createUser()));
}

void CreateUserWindow::createUser()
{
	if (_userPass->text() == _userConPass->text())
	{
		AgoraClient::AdminManager::GetAdminManager()->CreateUser(_userName->text().toStdString(),
			_userPass->text().toStdString());
		this->hide();
	}
	else
		QMessageBox::information(0, "Error", "You have made some mistakes in the password");
}